package com.hsnn.medstgmini.pro.base.service.Impl;


import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.base.dao.DrugShortageDao;
import com.hsnn.medstgmini.pro.base.model.BaseGoods;
import com.hsnn.medstgmini.pro.base.model.form.BaseShortmedicineForm;
import com.hsnn.medstgmini.pro.base.model.form.ShortMedicine;
import com.hsnn.medstgmini.pro.base.service.DrugShortageManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 已申请短缺药品
 * @author 罗超
 * @date 2016年3月7日10:58:01
 */
@Service
public class DrugShortageManagerImpl implements DrugShortageManager{
	@Autowired
	private DrugShortageDao shortageDao;
	@Autowired
	private HibernateSqlUtil dao;
	@Autowired
	private DrugShortageDao drugShortageDao;
	
	
	
	@Override
	public Pagination getShortageInfo(Pagination page , HospInfo hospInfo) {
		// TODO Auto-generated method stub
		page = shortageDao.getShortageInfo(page, hospInfo);
		@SuppressWarnings("unchecked")
		List<HashMap<String,Object>> list = (List<HashMap<String, Object>>) page.getRows();
		Date now =new Date();
		HashMap<String,Object> map =null;
		Date tempTime =new Date();
		for(int i=0,temp =list.size();i<temp;i++){
			boolean tag = true;
			 map = list.get(i);
			  tempTime =(Date) map.get("submit_datetime");
			if(tempTime!=null){
				  Calendar c = Calendar.getInstance();  
				  c.setTimeInMillis(tempTime.getTime());
				  c.add(Calendar.DATE, 30);
				  Date date =new Date(c.getTimeInMillis());
				tag = date.before(now);
			} 
			map.put("isdeclare", tag);	
		}
		return page ;
	}
	@Transactional
	public void UpdateBySMI(ShortMedicine sm) {
		shortageDao.UpdateBySMI(sm);
	}
	@Transactional
	public void UpdateBySS(ShortMedicine sm) {
		shortageDao.UpdateBySS(sm);
	}
	
	

	
	
	
	

	@Override
	public Pagination getDataList(Pagination page, HospInfo hospInfo) {
			page = drugShortageDao.getDataList(page, hospInfo);
			return page;
	}



	@Override
	public Map<String, Object> getDrugShortageDetail(String goodsCode, String projId) {
		return drugShortageDao.getDrugShortageDetail(goodsCode,projId);
	}
	
	@Transactional
	public void saveDrugInfoRep(BaseShortmedicineForm form, SysUser user) {
		drugShortageDao.saveDrugInfoRep(form, user);
	}
	
	@Transactional
	public void saveDrugInfo(BaseShortmedicineForm form, SysUser user) {
		drugShortageDao.saveDrugInfo(form, user);
	}
	
	
	public List<BaseGoods> getHospInfoListByDrugName(String drugName,String formName,String specName,String factor,String unitName,String compName,String drugAttr) {
		return drugShortageDao.getHospInfoByDrugName(drugName,formName,specName,factor,unitName,compName,drugAttr);
	}
	
	public Map<String, Object> getDrugInfo(String shortmedicine_id){
		return drugShortageDao.getDrugInfo(shortmedicine_id);
	}
	@Override
	public Pagination getAllShortage(Pagination page) {
		
		return drugShortageDao.getAllShortage(page);
	}
	@Override
	public Pagination getShortageInfoWSJ(Pagination page) {
		return drugShortageDao.getShortageInfoWSJ(page);
	}
	public List<BaseShortmedicineForm> getHospInfoListByDrugNameHosp(String drugName,String formName,String specName,String factor,String unitName,String compName,String drugAttr) {
		return drugShortageDao.getHospInfoListByDrugNameHosp(drugName,formName,specName,factor,unitName,compName,drugAttr);
	}

}
